/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treeship.core.service;

import java.io.File;

import treeship.core.exceptions.CantDoThatException;
import treeship.core.service.impl.ArchetypesRepositoryImpl;
import treeship.core.service.impl.DimensionalFactory1D;
import treeship.core.service.impl.LoggerImpl;
import treeship.core.service.impl.ShipFactoryImpl;
import treeship.core.service.xml.DocumentWrapper;
import treeship.core.service.xml.XMLReader;
import treeship.core.ship.impl.TreeShip;

/**
 * 
 * @author Moonchild
 */
public class Nexus {

	private static ShipFactoryImpl factory = new ShipFactoryImpl();
	private static ArchetypesRepositoryImpl repository = new ArchetypesRepositoryImpl();
	private static String hierarchyPath;
	private static LoggerImpl logger = new LoggerImpl();
	private static DimensionalFactory dimensionalFactory;

	public static ShipFactory<TreeShip> getFactory() {
		return factory;
	}

	public static ArchetypesRepository getRepository() {

		return repository;
	}

	public static LoggerImpl getLogger() {
		return logger;
	}

	public static void handleException(CantDoThatException ex) {
		logger.handleException(ex);
	}

	public static void softReset() {
		logger = new LoggerImpl();

	}

	private static void clearAll() {
		factory = new ShipFactoryImpl();
		repository = new ArchetypesRepositoryImpl();
		logger = new LoggerImpl();
		dimensionalFactory = null;
	}

	/**
	 * Startup with no parameters, to clear the repository of archetypes
	 */
	public static void startup() {
		clearAll();
	}

	/**
	 * Startup from file, to load a hierarchy, and with another
	 * DimensionalFactory
	 * 
	 * @param file
	 *            The file xml with the hierarchy
	 * @param normalize
	 *            if true, makes sure that the sum of weights of every child of
	 *            an entity equals 1
	 */
	public static void startup(File file, boolean normalize,
			DimensionalFactory df) {
		startup(file, normalize);
		dimensionalFactory = df;
	}

	/**
	 * Startup from file, to load a hierarchy, and with a Default 1d
	 * DimensionalFactory
	 * 
	 * @param file
	 *            The file xml with the hierarchy
	 * @param normalize
	 *            if true, makes sure that the sum of weights of every child of
	 *            an entity equals 1
	 */
	public static void startup(File file, boolean normalize) {

		clearAll();
		// hierarchyPath = hp;
		DocumentWrapper docWrap = null;
		if (dimensionalFactory == null) {
			dimensionalFactory = new DimensionalFactory1D();
		}

		docWrap = XMLReader.getWrappedDocumentFromXMLRealFile(file);

		ConfigurationParser parser = new ConfigurationParser(repository);
		parser.parseProcess(docWrap);

		if (normalize) {
			repository.normalizeWeights();
		}
	}

	/*
	 * @Deprecated public static void startup(String hp, boolean realOnoturl) {
	 * clearAll(); hierarchyPath = hp; DocumentWrapper docWrap = null; if
	 * (realOnoturl) { docWrap =
	 * XMLReader.getWrappedDocumentFromXMLFile(hierarchyPath); } else { docWrap
	 * = XMLReader.getWrappedDocumentFromResource(hierarchyPath); }
	 * processPropertyList(docWrap); processActionList(docWrap);
	 * processEntityList(docWrap); repository.normalizeWeights(); }
	 */

	public static DimensionalFactory getDimensionalFactory() {
		return dimensionalFactory;
	}
}
